﻿package problems.producerconsumer;

import problems.BenchmarkSemaphore;
import helpers.IAction;


/// <summary>
/// A solution for the Producer Consumer problem that contains a deadlock
/// </summary>
public class ProducerConsumerWithDeadlock extends AbstractProducerConsumer
{
    private final BenchmarkSemaphore mutex;
    private final BenchmarkSemaphore items;

    public ProducerConsumerWithDeadlock()
    {
        mutex = new BenchmarkSemaphore(1);
        items = new BenchmarkSemaphore(0);
    }

    @Override
    public void consume()
    {
        mutex.waitOne();
        items.waitOne();
        IAction action = getBuffer().remove();
        mutex.release();
        action.invoke();
    }

    @Override
    public void produce(IAction action)
    {
        mutex.waitOne();
        getBuffer().add(action);
        mutex.release();
        items.release();
    }
}
